package com.ynding.common.easypoi.model;

import cn.afterturn.easypoi.entity.ImageEntity;
import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.TemplateExportParams;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.PrintSetup;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Map;

/**
 * 根据模板导出excel工具类
 *
 * @author ynding
 */
@Slf4j
public class ExcelTemplateExportUtil {

    /**
     * 按指定大小导出模板
     *
     * @param templateName
     * @param data
     * @param fileOut
     * @param pageSize
     */
    public static void exportByTemplate(String templateName, Map<String, Object> data, OutputStream fileOut, short pageSize) {
        exportByTemplate(templateName, data, fileOut, true, pageSize);
    }

    /**
     * 只导出模板，不打印
     *
     * @param templateName
     * @param data
     * @param fileOut
     */
    public static void exportByTemplate(String templateName, Map<String, Object> data, OutputStream fileOut) {
        exportByTemplate(templateName, data, fileOut, false, (short) 0);
    }

    /**
     * @param templateName 模板名称
     * @param data         导出数据
     * @param fileOut
     * @param doPrint
     * @param pageSize
     */
    private static void exportByTemplate(String templateName, Map<String, Object> data, OutputStream fileOut, boolean doPrint, short pageSize) {
        TemplateExportParams params = new TemplateExportParams("export/template/" + templateName, true);
        Workbook workbook = null;
        try {
            workbook = ExcelExportUtil.exportExcel(params, data);
            if (workbook == null) {
                return;
            }
            if (doPrint) {
                Sheet sheet = workbook.getSheetAt(0);
                PrintSetup printSetup = sheet.getPrintSetup();
                printSetup.setLandscape(true);
                printSetup.setPaperSize(pageSize);
            }
            workbook.write(fileOut);
        } catch (Exception e) {
            log.error("", e);
        } finally {
            if (workbook != null) {
                try {
                    workbook.close();
                } catch (IOException e) {
                }
            }
        }
    }

    /**
     * 获取二维码，并生成图片实体类
     *
     * @param content
     * @param width
     * @param height
     * @return
     * @throws Exception
     */
    public static ImageEntity getQRCode(String content, int width, int height) throws Exception {
        // 插入图片
        ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
        BufferedImage bufferImg = QRCodeZxingUtil.createImage(content);
        ImageIO.write(bufferImg, "jpg", byteArrayOut);
        return new ImageEntity(byteArrayOut.toByteArray(), width, height);
    }

    /**
     * 读取图片url，并生成图片实体类
     *
     * @param url
     * @param width
     * @param height
     * @return
     * @throws IOException
     */
    public static ImageEntity getUrlImage(String url, int width, int height) throws IOException {
        // 插入图片
        ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
        BufferedImage bufferImg = ImageIO.read(new File(url));
        ImageIO.write(bufferImg, "jpg", byteArrayOut);
        return new ImageEntity(byteArrayOut.toByteArray(), width, height);
    }
}
